package cn.dnui_lf_705.dnui_16110100705.controller;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Scroller;

import cn.dnui_lf_705.dnui_16110100705.R;


public class ArActSlidLayout extends FrameLayout {
    // 页面边缘阴影的宽度默认值
    private static final int SHADOW_WIDTH = 16;
    private Activity mActivity;
    private Scroller mScroller; //安卓自带的一个滑动计算的类，只做计算，不参与逻辑；
    // 页面边缘的阴影图
    private Drawable mLeftShadow;
    // 页面边缘阴影的宽度
    private int mShadowWidth;

    private int mInterceptDownX;  //手指按下，拦截的x值
    private int mLastInterceptX;//记录最后一次坐标
    private int mLastInterceptY;

    private int mTouchDownX; //消费的x值
    private int mLastTouchX;
    private int mLastTouchY;

    private boolean isConsumed = false;//是否可以滑动

    public ArActSlidLayout(Context context) {
        this(context, null);
    }

    public ArActSlidLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArActSlidLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mScroller = new Scroller(context);
        mLeftShadow = getResources().getDrawable(R.drawable.shadow);//得到阴影的图形
        int density = (int) getResources().getDisplayMetrics().density;
        mShadowWidth = SHADOW_WIDTH * density;//得到实际像素的宽度；
    }

    /**
     * 绑定Activity
     */
    public void bindActivity(Activity activity) {
        mActivity = activity;
        ViewGroup decorView = (ViewGroup) mActivity.getWindow().getDecorView();
        View child = decorView.getChildAt(0);
        decorView.removeView(child);
        addView(child);
        decorView.addView(this);//把整个布局添加的窗口的ViewGroup中；
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {//进行事件的是否拦截
        boolean intercept = false;
        int x = (int) ev.getX();
        int y = (int) ev.getY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                intercept = false;
                mInterceptDownX = x;
                mLastInterceptX = x;
                mLastInterceptY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX = x - mLastInterceptX;
                int deltaY = y - mLastInterceptY;
                // 手指处于屏幕边缘，且横向滑动距离大于纵向滑动距离时，拦截事件
                if (mInterceptDownX < (getWidth() / 10) && Math.abs(deltaX) > Math.abs(deltaY)) {
                    intercept = true;//满足这个条件进行拦截；
                } else {
                    intercept = false;
                }
                mLastInterceptX = x;
                mLastInterceptY = y;
                break;
            case MotionEvent.ACTION_UP:
                intercept = false;
                mInterceptDownX = mLastInterceptX = mLastInterceptY = 0;//恢复数据；
                break;
        }
        //Log.e("event", " Intercep  " + "  x:  " + x + "  y:  " + y);
        return intercept;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {//事件的消费，具体逻辑的编写；
        int x = (int) ev.getX();
        int y = (int) ev.getY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTouchDownX = x;
                mLastTouchX = x;
                mLastTouchY = y;
                //Log.e("event", " onTouchEventDOWN  " + "  x:  " + x + "  y:  " + y);
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX = x - mLastTouchX;
                int deltaY = y - mLastTouchY;

                if (!isConsumed && mTouchDownX < (getWidth() / 10) && Math.abs(deltaX) > Math.abs(deltaY)) {
                    isConsumed = true; //移动的条件
                }

                if (isConsumed) {
                    int rightMovedX = mLastTouchX - (int) ev.getX();
                    // 左侧即将滑出屏幕
                    if (getScrollX() + rightMovedX >= 0) {
                        //移动到某一点
                        scrollTo(0, 0);
                    } else {
                        //相当于累加的移动
                        scrollBy(rightMovedX, 0); //手指滑动移动整个屏幕；负数：代表向右移动 ，反之，像做
                    }
                    // Log.e("event", " onTouchEventMOVE  " + "  x:  " + x + "  y:  " + y + " ScrollX:  " + getScrollX() + " rightMovedX: " + rightMovedX);
                }
                mLastTouchX = x;
                mLastTouchY = y;
                break;
            case MotionEvent.ACTION_UP:
                isConsumed = false;
                mTouchDownX = mLastTouchX = mLastTouchY = 0; //消除数据；
                // 根据手指释放时的位置决定回弹还是关闭
                if (-getScrollX() < getWidth() / 3) {
                    scrollBack();
                } else {
                    scrollClose();
                }
                //Log.e("event", " onTouchEventUP  " + "  x:  " + x + "  y:  " + y + " ScrollX:  " + getScrollX());
                break;
        }
        //Log.e("event"," onTouchEventAll  " + "  getRawX():  "+ev.getRawX() +"  y:  " +ev.getRawY());
        return true;
    }

    /**
     * 滑动返回
     */
    private void scrollBack() {
        int startX = getScrollX();
        int dx = -getScrollX();
        mScroller.startScroll(startX, 0, dx, 0, 300);
        invalidate();
    }

    /**
     * 滑动关闭
     */
    private void scrollClose() {
        int startX = getScrollX();
        int dx = -getScrollX() - getWidth();
        //Log.e("event", "  scrollClose:  dx " + dx + "  getWidth " + getWidth());
        mScroller.startScroll(startX, 0, dx, 0, 300);
        invalidate();
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), 0);
            //Log.e("event", "  computeScroll:  " + mScroller.getCurrX());
            postInvalidate();
        } else if (-getScrollX() >= getWidth()) {
            mActivity.finish();
            //Log.e("event", "  computeScroll: finish   " + getScrollX());
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        drawShadow(canvas);
    }

    /**
     * 绘制边缘的阴影
     */
    private void drawShadow(Canvas canvas) {
        mLeftShadow.setBounds(0, 0, mShadowWidth, getHeight());
        canvas.save();
        canvas.translate(-mShadowWidth, 0);
        mLeftShadow.draw(canvas);
        canvas.restore();
    }
}

